package cz.najman.emdroid.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cz.najman.emdroid.R;

public class HomeActivity extends ActionBarActivity{

	Button btEventsAvailable;
	Button btEventsRegistered;
	Button btEventsManage;
	Button btAccount;
	Button btSettings;
	Context mContext;
	SharedPreferences mPreferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		btEventsManage = (Button) findViewById(R.id.home_bt_event_manage);
		btEventsAvailable = (Button) findViewById(R.id.home_bt_event_available);
		btEventsRegistered = (Button) findViewById(R.id.home_bt_event_registered);
		btAccount = (Button) findViewById(R.id.home_bt_account);
		btSettings = (Button) findViewById(R.id.home_bt_settings);		
		setActionBarTitle(R.string.app_name);
		//setActionBarHomeAction( new Intent(this, HomeActivity.class) );
				
		btEventsRegistered.setOnClickListener(new btEventsRegisteredOnClickListener());	
		btSettings.setOnClickListener(new btPreferencesOnClickListener());
		btEventsAvailable.setOnClickListener(new btEventsAvailableOnClickListener());
		mContext = this;
		mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);		
					
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		boolean isUserLoged = mPreferences.getBoolean("isUserLoged", false);
		
		if(!isUserLoged){
			Intent intent = new Intent(mContext, LoginActivity.class);
			startActivity(intent);
		}
		
	}
			
	public class btEventsAvailableOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, AvailableEventsActivity.class);
			int userId = mPreferences.getInt("userId", -1);
			intent.putExtra("userId", userId);
			mContext.startActivity(intent);			
		}
		
	}
	
	public class btEventsRegisteredOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, RegisteredEventsActivity.class);
			int userId = mPreferences.getInt("userId", -1);
			intent.putExtra("userId", userId);
			mContext.startActivity(intent);			
		}
		
	}
	
	public class btPreferencesOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, LoginActivity.class);
			mContext.startActivity(intent);			
		}
		
	}
	
}
